package com.example.base;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 业务层增删改查基类
 */
public abstract class BaseCrudService<M extends BaseCrudDao<T>, T extends BaseEntity> {

    @Autowired
    private M mapper;

    /**
     * 保存数据，新增或修改
     * @param entity
     */
    public void save(T entity){
        if(StrUtil.isEmpty(entity.getId())){
            //新增
            entity.setId(IdUtil.simpleUUID());
            mapper.insert(entity);
        }else{
            //修改
            mapper.update(entity);
        }
    }

    /**
     * 删除
     */
    public void delete(String id){
        mapper.delete(id);
    }

    /**
     * 实体类查询
     */
    public T get(String id){
        return mapper.get(id);
    }

    /**
     * 列表查询
     */
    public List<T> findList(T entity){
        return mapper.findList(entity);
    }

    /**
     * 记录总条数查询
     */
    public Integer findAllCount(T entity){
        return mapper.findAllCount(entity);
    }

}
